package cn.graydove.bot.data;

import cn.graydove.bot.adapter.GroupMessageEvent;
import cn.graydove.bot.adapter.RobotClient;
import cn.graydove.bot.model.entity.Mock;
import cn.graydove.bot.model.entity.QqGroup;
import cn.graydove.bot.model.entity.User;
import cn.graydove.bot.service.GroupService;
import cn.graydove.bot.utils.CommonUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class GroupServiceCacheData {

    private GroupService groupService;

    public GroupServiceCacheData(GroupService groupService) {
        this.groupService = groupService;
    }


    public void setTree(QqGroup group, Long userId, boolean isOnTree) {
        User user = findUser(group, userId);
        user.setOnTree(isOnTree);
        groupService.saveUser(user);
    }

    public void grant(QqGroup group, Long userId) {
        groupService.grant(group, userId);
    }

    public void revoke(QqGroup group, Long userId) {
        groupService.revoke(group, userId);
    }

    public QqGroup getGroup(Long groupId) {
        return groupService.findByGroupNumber(groupId);
    }

    public void start(Long groupId) {
        QqGroup group = getGroup(groupId);
        group.setAlive(true);
        groupService.save(group);
    }

    public void stop(Long groupId) {
        QqGroup group = getGroup(groupId);
        group.setAlive(false);
        groupService.save(group);
    }

    public void setAttacking(QqGroup group, Long attacker) {
        group.setAttacker(attacker);
        groupService.save(group);
    }

    public User getAttacker(QqGroup group) {
        Long attackerId = group.getAttacker();
        return findUser(group, attackerId);
    }

    public User getAttacker(Long groupId) {
        return getAttacker(getGroup(groupId));
    }


    public boolean isAttacking(Long groupNumber) {
        return groupService.isAttacking(groupNumber);
    }

    public QqGroup ensureUserAndGetGroup(GroupMessageEvent event) {
        QqGroup group = getGroup(event.getGroupId());
        if (group == null) {
            return null;
        }
        User user = findUser(group, event.getUserId());
        if (user == null) {
            user = new User(event.getUserId(), CommonUtils.getName(event));
            group = groupService.addUser(group, user);
        } else {
            String name = CommonUtils.getName(event);
            if (!user.getName().equals(name)) {
                user.setName(name);
                groupService.saveUser(user);
            }
        }

        return group;
    }

    public QqGroup ensureUserAndGetGroup(QqGroup group, RobotClient client, long userId) {
        if (group == null) {
            return null;
        }
        User user = findUser(group, userId);
        if (user == null) {
            user = new User(userId, CommonUtils.getName(client.getGroupMemberInfo(group.getGroupNumber(), userId)));
            group = groupService.addUser(group, user);
        }
        return group;
    }

    public QqGroup addUser(QqGroup group, User user) {
        return groupService.addUser(group, user);
    }

    public User findUser(Long groupNumber, Long qqNumber) {
        return findUser(getGroup(groupNumber), qqNumber);
    }

    public User findUser(QqGroup group, Long qqNumber) {
        if (qqNumber == null) {
            return null;
        }
        for (var u : group.getUsers()) {
            if (qqNumber.equals(u.getQqNumber())) {
                return u;
            }
        }
        return null;
    }

    public void removeMocks(QqGroup group, Collection<Mock> mocks) {
        groupService.removeMocks(group, mocks);
    }

    public void addMock(QqGroup group, Mock mock) {
        groupService.addMock(group, mock);
    }

    public void clearAttack() {
        groupService.clearAttack();
    }

    public User saveUser(User u) {
        return groupService.saveUser(u);
    }

    public QqGroup saveGroup(QqGroup qqGroup) {
        return groupService.save(qqGroup);
    }

    public boolean isOpen(Long groupId) {
        return groupService.isOpen(groupId);
    }
}
